home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / DigestOutputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.0 KB  |  164 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)DigestOutputStream.java    1.23 98/06/29
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.io.IOException;
  18. import java.io.EOFException;
  19. import java.io.OutputStream;
  20. import java.io.FilterOutputStream;
  21. import java.io.PrintStream;
  22. import java.io.ByteArrayOutputStream;
  23.  
  24. /**
  25.  * A transparent stream that updates the associated message digest using
  26.  * the bits going through the stream.
  27.  *
  28.  * <p>To complete the message digest computation, call one of the
  29.  * <code>digest</code> methods on the associated message
  30.  * digest after your calls to one of this digest ouput stream's
  31.  * {@link write(int) write} methods.
  32.  *
  33.  * <p>It is possible to turn this stream on or off (see
  34.  * {@link on(boolean) on}). When it is on, a call to one of the
  35.  * <code>write</code> methods results in
  36.  * an update on the message digest.  But when it is off, the message
  37.  * digest is not updated. The default is for the stream to be on.
  38.  *
  39.  * @see MessageDigest
  40.  * @see DigestInputStream
  41.  *
  42.  * @version 1.23 99/03/26
  43.  * @author Benjamin Renaud */
  44.  
  45. public class DigestOutputStream extends FilterOutputStream {
  46.  
  47.     private boolean on = true;
  48.  
  49.     /**
  50.      * The message digest associated with this stream.
  51.      */
  52.     protected MessageDigest digest;
  53.  
  54.     /**
  55.      * Creates a digest output stream, using the specified output stream
  56.      * and message digest.
  57.      *
  58.      * @param stream the output stream.
  59.      *
  60.      * @param digest the message digest to associate with this stream.
  61.      */
  62.     public DigestOutputStream(OutputStream stream, MessageDigest digest) {
  63.     super(stream);
  64.     setMessageDigest(digest);
  65.     }
  66.  
  67.     /**
  68.      * Returns the message digest associated with this stream.
  69.      *
  70.      * @return the message digest associated with this stream.
  71.      */
  72.     public MessageDigest getMessageDigest() {
  73.     return digest;
  74.     }
  75.  
  76.     /**
  77.      * Associates the specified message digest with this stream.
  78.      *
  79.      * @param digest the message digest to be associated with this stream.
  80.      */
  81.     public void setMessageDigest(MessageDigest digest) {
  82.     this.digest = digest;
  83.     }
  84.  
  85.     /**
  86.      * Updates the message digest (if the digest function is on) using
  87.      * the specified byte, and in any case writes the byte
  88.      * to the output stream. That is, if the digest function is on
  89.      * (see {@link on(boolean) on}), this method calls
  90.      * <code>update</code> on the message digest associated with this
  91.      * stream, passing it the byte <code>b</code>. This method then
  92.      * writes the byte to the output stream, blocking until the byte
  93.      * is actually written.
  94.      *
  95.      * @param b the byte to be used for updating and writing to the
  96.      * output stream.
  97.      *
  98.      * @exception IOException if an I/O error occurs.
  99.      *
  100.      * @see MessageDigest#update(byte)
  101.      */
  102.     public void write(int b) throws IOException {
  103.     if (on) {
  104.         digest.update((byte)b);
  105.     }
  106.     out.write(b);
  107.     }
  108.  
  109.     /**
  110.      * Updates the message digest (if the digest function is on) using
  111.      * the specified subarray, and in any case writes the subarray to
  112.      * the output stream. That is, if the digest function is on (see
  113.      * {@link on(boolean) on}), this method calls <code>update</code>
  114.      * on the message digest associated with this stream, passing it
  115.      * the subarray specifications. This method then writes the subarray
  116.      * bytes to the output stream, blocking until the bytes are actually
  117.      * written.
  118.      *
  119.      * @param b the array containing the subarray to be used for updating
  120.      * and writing to the output stream.
  121.      *
  122.      * @param off the offset into <code>b</code> of the first byte to
  123.      * be updated and written.
  124.      *
  125.      * @param len the number of bytes of data to be updated and written
  126.      * from <code>b</code>, starting at offset <code>off</code>.
  127.      *
  128.      * @exception IOException if an I/O error occurs.
  129.      *
  130.      * @see MessageDigest#update(byte[], int, int)
  131.      */
  132.     public void write(byte[] b, int off, int len) throws IOException {
  133.     if (on) {
  134.         digest.update(b, off, len);
  135.     }
  136.     out.write(b, off, len);
  137.     }
  138.  
  139.     /**
  140.      * Turns the digest function on or off. The default is on.  When
  141.      * it is on, a call to one of the <code>write</code> methods results in an
  142.      * update on the message digest.  But when it is off, the message
  143.      * digest is not updated.
  144.      *
  145.      * @param on true to turn the digest function on, false to turn it
  146.      * off.
  147.      */
  148.     public void on(boolean on) {
  149.     this.on = on;
  150.     }
  151.  
  152.     /**
  153.      * Prints a string representation of this digest output stream and
  154.      * its associated message digest object.
  155.      */
  156.      public String toString() {
  157.      return "[Digest Output Stream] " + digest.toString();
  158.      }
  159. }
  160.  
  161.  
  162.  
  163.  
  164.